-- TitleRender

local ClassRef = app.mvc.createRenderClass(...)

--data = RareVo
function ClassRef.create(data, node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.NewBagTitleNode))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init(data)
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	self._onBtnClick = nil
	self:setContentSize(self._ui["Panel"]:getContentSize())

	self._ui["Panel/selected_btn"]:addClickEventListener(function()
			if self._data[2] then
				if UD:isNewGotTitle( self._data[2]:getId() ) then
					UD:clearNewGotTitle(self._data[2]:getId())
				end
				self:setSelectedBtnBrightStatus( not self:getIsSelected() )
				executeFunc(self._onBtnClick, "TitleRender_changeSelectedStatus", self._data[2])
				UD.redPoint:checkStateByKey(Constant.FUNC_TYPE_PACK_TITLE)
			end
		end)

	self._name = self._ui["Panel/name"] -- 名字
	self._text = self._ui["Panel/Text"] -- 获取途径
	self._icon = self._ui["Panel/icon_1"] -- 激活之后的icon

	self._unLockIcon = self._ui["Panel/icon_2"] -- 未激活icon

	self:refresh()
end

-- data = { dataidx-数据源索引, Title Struct }
function ClassRef:refresh()
	if self._data and self._data[2] then
		self:setVisible( true )

		local iconPath = GD:getImagePath( Res.medalIconPath, self._data[2]:getIcon() )
		self._icon:loadTexture( iconPath )

		self._ui["Panel/bg"]:setVisible( 0 == tonumber( self._data[1] )%2 ) -- 偶数ID，bg不可见

		self._name:setString( L(self._data[2]:getName()) )
		self._text:setString( L(self._data[2]:getDesc()) )

		self._ui["Panel/time"]:hide()
		if UD:isGotTitle( self._data[2]:getId() ) then -- 解锁？
			self._name:setOpacity(255)
			self._icon:setVisible( true )
			self._unLockIcon:setVisible( false )

			self:setColor( display.COLOR_WHITE )
			self:setSelectedBtnClickable( true )
			self:setGetWayVisible( false )

			if UD:isTitleActive( self._data[2]:getId() ) then -- 选用？
				self:setSelectedBtnBrightStatus( false )
			else
				self:setSelectedBtnBrightStatus( true )
			end
			if not self:getIsSelected() then -- 已经选用
				self:setUsedVisible( true )
				self:setUnUsedVisible( false )
			else
				self:setUsedVisible( false )
				self:setUnUsedVisible( true )
			end
			if UD:isNewGotTitle( self._data[2]:getId() ) then -- 新获得？
				self:setNewFlagVisible( true )
			else
				self:setNewFlagVisible( false )
			end

			local spt = UD:getSpTitles()
			if spt and spt[self._data[2]:getId()] and spt[self._data[2]:getId()].duration > 0 then
				local seconds = spt[self._data[2]:getId()].duration - app.session:getServerTime()
				if seconds > 0 then
					self._ui["Panel/time"]:show()
					self._ui["Panel/time"]:setString(TimeUtil.deltaSecondsToTimeString(seconds))
				end
			end
		else
			self._name:setOpacity(100)
			self._icon:setVisible( false )
			self._unLockIcon:setVisible( true )

			self:setColor( cc.c3b( 200, 200, 200 ) )
			-- self:setColor( display.COLOR_GRAY )

			self:setSelectedBtnClickable( false )
			self:setGetWayVisible( true )

			self:setSelectedBtnBrightStatus( true ) -- 选用状态一定是否
			self:setUsedVisible( false )
			self:setUnUsedVisible( false )

			self:setNewFlagVisible( false )
		end
	else
		self:setVisible( false )
	end
end

-- 设置 selected_btn 的 Bright 状态
function ClassRef:setSelectedBtnBrightStatus( b )
	self._ui["Panel/selected_btn"]:setBright( b )
end
-- 是否选中
function ClassRef:getIsSelected()
	return self._ui["Panel/selected_btn"]:isBright()
end
-- 是否可点击
function ClassRef:setSelectedBtnClickable( b )
	self._ui["Panel/selected_btn"]:setEnabled( b )
	self._ui["Panel/selected_btn"]:setVisible( b )
end
-- 是否新获得
function ClassRef:setNewFlagVisible( b )
	self._ui["Panel/dot"]:setVisible( b )
end
function ClassRef:setUsedVisible( b )
	self._ui["Panel/Text_use"]:setVisible( b )
end
function ClassRef:setUnUsedVisible( b )
	self._ui["Panel/Text_unused"]:setVisible( b )
end
function ClassRef:setGetWayVisible( b )
	self._text:setVisible( b )
end

function ClassRef:onBtnClick(cb)
	self._onBtnClick = cb
end

return ClassRef
